package com.tablefood.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.tablefood.dao.UserDao;
import com.tablefood.dao.Impl.UserDaoImpl;
import com.tablefood.domain.ShopUser;
import com.tablefood.service.UserService;
 
import com.tablefood.utils.PageUserBean;

@Transactional
public class UserServiceImpl implements UserService {

	@Resource
	UserDao userDao;
	 
	//添加用户
	@Override
	public boolean addUser(ShopUser user) { 
		System.out.println("进入userDaoImpl的insertUser方法中添加新用户");
		return userDao.addUser(user);
	}

	//更新用户
	@Override
	public boolean updateUser(ShopUser user) {
		System.out.println("进入userDaoImpl的updateuser方法中更新用户");
		return userDao.updateUser(user);
	}

	//更改用户登录权限
	@Override
	public boolean delUser(final Long userid) {
		return userDao.delUser(userid);
	}

	//通过用户名查找用户
	@Override
	public List searchUser(String whereSql) {
		System.out.println("进入userDaoImpl的searchUser方法中查找用户");
		return userDao.searchUser(whereSql);
	}

	//检索用户名是否重复
	@Override
	public boolean checkUserName(String name) {
		 
		return userDao.checkUserName(name);
	}

	//通过userid查找用户
	@Override
	public ShopUser getUserByUserId(Long userid) {
		 
		return userDao.getUserByUserId(userid);
	}

	@Override
	public PageUserBean queryForPage(int pageSize, int page) {
		
		final String hql = "from ShopUser u where u.status = '1' order by u.userid desc"; // 查询语句
		
		int allRow = userDao.getAllRowCount(hql); // 总记录数
		
		int totalPage = PageUserBean.countTatalPage(pageSize, allRow); // 总页数
		final int offset = PageUserBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageUserBean.countCurrentPage(page); // 当前页
		List list = userDao.queryForPage(hql, offset, length); //

		// 把分页信息保存到Bean当中
		PageUserBean pageUserBean = new PageUserBean();
		pageUserBean.setPageSize(pageSize);
		pageUserBean.setCurrentPage(currentPage);
		pageUserBean.setAllRow(allRow);
		pageUserBean.setTotalPage(totalPage);
		pageUserBean.setList(list);
		pageUserBean.init();
		return pageUserBean;
		 
	}

	 

	 

	

}
